package com.schoolcloud.social.controller;

import com.schoolcloud.common.model.R;
import com.schoolcloud.common.util.UserContext;
import com.schoolcloud.social.dto.PrivateMessageSaveDTO;
import com.schoolcloud.social.entity.PrivateMessage;
import com.schoolcloud.social.service.ConversationParticipantsService;
import com.schoolcloud.social.service.PrivateMessageService;
import com.schoolcloud.social.vo.ConversationInfoVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("social/chat/private-message")
@RequiredArgsConstructor
public class PrivateMessageController {
    private final PrivateMessageService privateMessageService;
    private final ConversationParticipantsService conversationParticipantsService;

    @PostMapping
    public R createPrivateMessage(@RequestBody PrivateMessageSaveDTO privateMessage) {
        conversationParticipantsService.createMessage(privateMessage);
        return R.ok();
    }
    @GetMapping("/conversation-list")
    public R<List<ConversationInfoVO>> getUserConversationList() {
        List<ConversationInfoVO> list=conversationParticipantsService.getConversationListByUserId(UserContext.getUser());
        return R.ok(list);
    }
    @GetMapping("/list/by/conversation/{conversationId}")
    public R<List<PrivateMessage>> getPrivateMessageListByConversationId(@PathVariable Integer conversationId) {
        List<PrivateMessage> list=privateMessageService.getPrivateMessageListByConversationId(conversationId);
        return R.ok(list);
    }

}
